
package com.trydofor.a9task.base.sys.help;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.reflections.Reflections;
import org.reflections.scanners.FieldAnnotationsScanner;

import com.trydofor.a9task.base.sys.anno.SystemData;
import com.trydofor.aloe.exception.Exceptions;

/**
 * @author : Shi Rongjiu (www.trydofor.com)
 * @date : Nov 23, 2012
 * @see :
 */

public class SystemDataLoader {
    
    public static void load(Object... args) {
        List<FieldFilter> filters = new ArrayList<FieldFilter>();
        for (Object obj : args) {
            if (obj instanceof FieldFilter) {
                filters.add((FieldFilter) obj);
            }
        }
        
        Reflections rf = new Reflections(new FieldAnnotationsScanner(), args);
        Set<Field> fds = rf.getFieldsAnnotatedWith(SystemData.class);
        for (Field fd : fds) {
            for (FieldFilter ft : filters) {
                ft.filter(fd);
            }
        }
    }
    
    public static void load(List<FieldFilter> filters, String... packages) {
        Exceptions.checkNullPointer(packages);
        Exceptions.checkNullPointer(filters);
        
        List<Object> args = new ArrayList<Object>(packages.length + filters.size() + 1);
        args.add(new FieldAnnotationsScanner());
        args.addAll(filters);
        for (String pk : packages) {
            args.add(pk);
        }
        
        Reflections rf = new Reflections(args);
        Set<Field> fds = rf.getFieldsAnnotatedWith(SystemData.class);
        for (Field fd : fds) {
            for (FieldFilter ft : filters) {
                ft.filter(fd);
            }
        }
    }
}
